package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar;
import com.apps.ftapp.R;
import com.apps.ftapp.model.SubjectModel;
import com.apps.ftapp.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;


/**
 * Created by lyli on 2017/10/4.
 */

public class SubjectNewAdapter extends BaseAdapter {


    private Context context;
    private List<SubjectModel.CategoriesBean> dataList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private String type;


    public SubjectNewAdapter(Context context, List dataList, String type) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);
        this.type = type;


        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }

    public void addRes(List<SubjectModel.CategoriesBean> data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        SubjectModel.CategoriesBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.item_week_new, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

//
        imageLoader.displayImage(bean.getIcon(), viewHolder.subjectImg, options);
//        if("教师招聘".equals(type)){
//
//            viewHolder.subjectImg.setImageDrawable(context.getResources().getDrawable(R.drawable.f_tk_bian));
//        }else{
//            viewHolder.subjectImg.setImageDrawable(context.getResources().getDrawable(R.drawable.f_tk_zheng));
//        }

        viewHolder.subjectTitle.setText(bean.getName());
        viewHolder.subject_jindu_num.setText(bean.getDo_questions() + "/" + bean.getQuestions());
//        viewHolder.subject_top.setOnClickListener(this);
        viewHolder.subject_top.setTag(i);

        viewHolder.subject_jindu.setMax(Tools.isFloatNull(bean.getQuestions()) == 0 ? 1 : Tools.isFloatNull(bean.getQuestions()));
        viewHolder.subject_jindu.setProgress(Tools.isFloatNull(bean.getDo_questions()) == 0 ? 1 : Tools.isFloatNull(bean.getDo_questions()));
////        viewHolder.mTagContainerLayout.setTheme(ColorFactory.NONE);
////        viewHolder.mTagContainerLayout.setTagBackgroundColor(Color.TRANSPARENT);
//        List<String> tags = new ArrayList<>();
//        if(null!=bean.getBiaoqian()&&bean.getBiaoqian().size()>0){
//            for(SubjectModel.CategoriesBean.BiaoqianBean b:bean.getBiaoqian()){
//                tags.add(b.getName());
//            }
//        }
////        for(bean.getBiaoqian())
//        viewHolder.mTagContainerLayout.setTags(tags);

        return view;
    }


//    class ViewHolder {
//        ImageView subjectImg;
//        TextView subjectTitle;
//        TagContainerLayout mTagContainerLayout;
//        TextView lessonTime;
//        TextView lessonSale;
//        TextView lessonPrice;
//        public ViewHolder(View view) {
//            subjectImg = (ImageView) view.findViewById(R.id.subject_img);
//            subjectTitle = (TextView) view.findViewById(R.id.subject_title);
//            mTagContainerLayout = (TagContainerLayout) view.findViewById(R.id.tagcontainerLayout);
////            mTagContainerLayout.setTags(List<String> tags);
//          //  lessonPrice = (TextView) view.findViewById(R.id.lesson_price);
//            //lessonTime = (TextView) view.findViewById(R.id.lesson_time);
//         //   lessonSale = (TextView) view.findViewById(R.id.lesson_sale);
//        }
//    }


    class ViewHolder {
        ImageView subjectImg;
        TextView subjectTitle;
        RoundCornerProgressBar subject_jindu;
        TextView subject_jindu_num;

        LinearLayout subject_top;

        public ViewHolder(View view) {
            subjectImg = view.findViewById(R.id.subject_img);
            subjectTitle = view.findViewById(R.id.subject_title);
            subject_jindu = view.findViewById(R.id.subject_jindu);
            subject_jindu_num = view.findViewById(R.id.subject_jindu_num);

            subject_top = view.findViewById(R.id.subject_top);
        }
    }


}
